<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<div class="row">
    <div class="col-lg-12">
        <h3 class="page-header">Chỉnh sửa chi tiết đơn hàng</h3>
    </div>
    <!-- /.col-lg-12 -->
</div>
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <div class="row">
                    <div class="col-lg-12">
                        <form method="POST">            
                            <div class="form-group">                                
                                <button type="button" class="btn btn-default" onclick="window.location = '<?php echo $this->baseUrl()."/default/order/detail/id/".$this->item->id ?>'" >Quay lại</button>
                            </div>                          
                                <div class="form-group">
                                    <div class="row">
                                        <div class="col-lg-2">
                                            Mã đơn hàng
                                        </div>
                                        <div class="col-lg-4">
                                            <input name="order_no" type="text" class="form-control" value="<?php echo $this->item->order_no ?>" readonly="true">
                                        </div>
                                        <div class="col-lg-2">
                                            Tên cửa hàng
                                        </div>
                                        <div class="col-lg-4">
                                            <input type="text" class="form-control" value="<?php echo GlobalLib::getName("agent",$this->item->store_id,'name') ?>" readonly="true">
                                        </div>
                                    </div>
                                </div>                               
                                <div class="form-group">
                                    <div class="row">                                       
                                        <div class="col-lg-2">
                                            Ngày tạo
                                        </div>
                                        <div class="col-lg-4">
                                            <input type="text" class="form-control" value="<?php echo GlobalLib::viewDate($this->item->created)?>"readonly="true">
                                        </div>
                                    </div>
                                </div>                                                                                                                      
                             <div class="form-group">
                                <div class="row">
                                     <div class="col-lg-2">
                                        <label>Tỉnh thành</label>
                                    </div>
                                     <div class="col-lg-4">
                                        <?php echo $this->provinceHTML; ?>
                                    </div> 
                                     <div class="col-lg-2">
                                        <label>Quận huyện</label>
                                    </div>
                                     <div class="col-lg-4">
                                        <?php echo $this->districtHTML; ?>
                                    </div> 
                                </div> 
                                </div>
                                <div class="form-group">
                                <div class="row">                                     
                                    <div class="col-lg-2">
                                        <label>DS Đại lý</label>
                                    </div>
                                     <div class="col-lg-4">
                                        <?php echo $this->agentHTML; ?>
                                    </div>                  
                                    <div class="col-lg-2">
                                        <label>Ngày mua hàng</label>
                                    </div>
                                    <div class="col-lg-4">
                                        <input class="form-control" type="datetime" name="order_created" id="order_created" value="<?php echo GlobalLib::viewDate($this->item_detail->order_created) ?>" >
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    
                                    <div class="col-lg-2">
                                        <label>DS Sản phẩm</label>
                                    </div>
                                      <div class="col-lg-4">
                                        <?php echo $this->productHTML ?>
                                    </div>       
                                     <div class="col-lg-2">
                                        <label>Số lượng</label>
                                    </div>
                                    <div class="col-lg-4" id="quantity_id">
                                        <input value="<?php echo $this->item_detail->quantity ?>" id="quantity" class="form-control" name="quantity" onkeydown="change_text();" onkeyup="change_text();">
                                    </div>
                                </div>
                            </div>
                             <div class="form-group">
                                <div class="row">                                     
                                     
                                    <div class="col-lg-2">
                                        <label>Hệ số điểm</label>
                                    </div>
                                    <div class="col-lg-4" id="factor_id">
                                        <input value="<?php echo $this->item_detail->factor ?>" id="factor" class="form-control" name="factor" onkeydown="change_text();" onkeyup="change_text();" readonly="true">
                                    </div>       
                                    <div class="col-lg-2">
                                        <label>Điểm nhận được</label>
                                    </div>
                                      <div class="col-lg-4" id="charges_id">
                                          <input value="<?php echo $this->item_detail->charges ?>" id="charges" class="form-control" name="charges" readonly="true"> 
                                      </div>
                                </div>
                            </div>             
                            <input type="hidden" id="submit_type" name="submit_type" value="2" />
                            <div class="form-group">
                                <button type="submit" onclick="return checkadd(1);" class="btn btn-primary">Lưu</button>
                                <button type="reset" class="btn btn-default">Đặt lại</button>                                
                            </div>                                                            
                        </form>
                        <div class="col-lg-12">
                            <div class="row">
                                <div id="jqxgrid"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>   
</div>    
<script type="text/javascript">
        $(document).ready(function () {
            $("#quantity").keydown(function (e) {
                    // Allow: backspace, delete, tab, escape, enter and .
                    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
                         // Allow: Ctrl+A
                        (e.keyCode == 65 && e.ctrlKey === true) || 
                         // Allow: home, end, left, right
                        (e.keyCode >= 35 && e.keyCode <= 39)) {
                             // let it happen, don't do anything
                             return;
                    }
                    // Ensure that it is a number and stop the keypress
                    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                        e.preventDefault();
                    }
                });
            // prepare the data
            var source =
            {
                datatype: "json",
                datafields:
                [
                    { name: 'id',type:'int'},
                    { name: 'product_name', type: 'string' },     
                    { name: 'quantity', type: 'string' },
                    { name: 'order_created', type: 'date'},
                    { name:'price',type:'float'},
                    { name:'factor',type:'float'},
                    { name:'charges',type:'float'},
                    { name:'agent_name',type:'string'},
                    {name: 'order_status_name', type:'string'},
                    {name: 'order_status_id', type:'int'}
                ],
                url: "<?php echo $this->baseUrl().'/default/order/servicedetail/order_id/'.$this->item->order_no ?>",
                sortcolumn: 'order_area',
                sortdirection: 'asc'
            };
         var dataAdapter = new $.jqx.dataAdapter(source, {
                loadComplete: function () {
                }
            });
            $("#jqxgrid").jqxDataTable(
            {
                source: dataAdapter,
                theme:'bootstrap',
                pageable: true,
                altRows: true,
                sortable: false,                
                width: '100%',                
                ready: function () {
                    $("#jqxgrid").jqxDataTable('hideColumn', 'id');
                },
                columns: [ 
                  { text: 'id',hidden:true,dataField:'id',cellsalgin:'right',width:0},
                  { text: 'order_status_id',hidden:true,dataField:'order_status_id',cellsalgin:'right',width:0},
                  { text: 'Tên sản phẩm',hidden:false,dataField:'product_name',cellsalgin:'right',width:'25 %'},
                  { text: 'Ngày mua hàng',hidden:false,dataField:'order_created',cellsalgin:'right',width:'15%',cellsFormat:'dd-MM-yyyy'},                  
                  { text: 'Số lượng',hidden:false,dataField:'quantity',cellsalgin:'right',width:'10%'},                  
                  { text: 'Tên đại lý',hidden:false,editable: false, dataField: 'agent_name', width: '15%' },
                  { text: 'Số điểm', editable: false, dataField: 'factor', width:'10%' },
                  { text: 'Số điểm nhận được', editable: false, dataField: 'charges', width:'15%' },
                  { text: 'Trạng thái', editable: false, dataField: 'order_status_name', width:'10%' }                   
                ]
            });
           
        });
    </script>
<script type="text/javascript">
$(document).ready(function(){           
           $("#order_created").datepicker({
                dateFormat: "dd-mm-yy",
               onSelect: function() {
                                   var params = {
                    action:'getproduct',
                    agent_id: $('#agent_id').val(),
                    product_id:$('#product_id').val(),
                    order_created:$('#order_created').val()
                };
                $.post('<?php echo $this->baseUrl() ?>/default/service/index', params,
                            function (data) {
                                 var items = eval(data);
                                var jItem = items[0];
                               $('#product_id').html(jItem.html);
                               $('#charges_id').html(jItem.html3);
                               $('#factor_id').html(jItem.html1);
                               $('#quantity_id').html(jItem.html2);
                            });
            }
            });
        });
function checkadd(type)        
{    
    if($("#agent_id").val()=="0")
    {
        bootbox.alert("Vui lòng chọn đại lý!");
        return false;
    }
    if($("#order_created").val()=="")
    {
        bootbox.alert("Vui lòng chọn ngày mua hàng!");
        return false;
    }
    if($("#product_id").val()=="0" || $("#product_id").val()=="")
    {
        bootbox.alert("Vui lòng chọn sản phẩm!");
        return false;
    }    
    if($("#quantity").val()=="" || isNaN($("#quantity").val())==true || $("#quantity").val()=="0")
    {
        bootbox.alert("Vui lòng nhập số lượng!");
        return false;
    }
    $("#submit_type").val(type);
};
function change_text()
{
    var quantity= $('#quantity').val();
    var factor=$('#factor').val();
    var charges=quantity*factor;
    document.getElementById('charges').value=charges;
}
</script>